from django.test import TestCase

import json
from django.contrib.auth.models import User
from .models import Payment
from .views import create_payment_with_order, delete_payment_and_detach_order, update_payment_order
from Shop.models import Order, Commodity, OrderShip

class PaymentTestCase(TestCase):
    def setUp(self):
        # 创建用户
        self.user = User.objects.create_user(username='testuser', password='testpass')

        # 创建订单
        self.order = Order.objects.create()  # 请替换为你的订单模型和创建订单的逻辑

    def test_create_payment_with_order(self):
        # 创建支付记录并关联订单
        data = {
            'user_id': self.user.id,
            'order_id': self.order.id,
            'amount': 100.0,
            'payment_method': 'Credit Card',
        }

        response = create_payment_with_order(self.create_request('POST', data))
        self.assertEqual(response.status_code, 200)

        # 在这里你可以添加更多的断言来验证支付记录的创建逻辑

    # def test_delete_payment_and_detach_order(self):
    #     # 创建支付记录并关联订单
    #     payment = Payment.objects.create(user=self.user, order=self.order, amount=100.0, payment_method='Credit Card')
    #
    #     # 删除支付记录并解除与订单的关联
    #     response = delete_payment_and_detach_order(self.create_request('DELETE'), payment_id=payment.id)
    #     self.assertEqual(response.status_code, 200)
    #
    #     # 在这里你可以添加更多的断言来验证支付记录的删除逻辑和订单的解除关联逻辑
    #
    # def test_update_payment_order(self):
    #     # 创建两个订单
    #     order1 = YourOrderModel.objects.create(...)  # 请替换为你的订单模型和创建订单的逻辑
    #     order2 = YourOrderModel.objects.create(...)  # 请替换为你的订单模型和创建订单的逻辑
    #
    #     # 创建支付记录并关联到第一个订单
    #     payment = Payment.objects.create(user=self.user, order=order1, amount=100.0, payment_method='Credit Card')
    #
    #     # 修改支付记录关联的订单到第二个订单
    #     data = {
    #         'new_order_id': order2.id,
    #     }
    #
    #     response = update_payment_order(self.create_request('PUT', data), payment_id=payment.id)
    #     self.assertEqual(response.status_code, 200)
    #
    #     # 在这里你可以添加更多的断言来验证支付记录的订单关联更新逻辑
    #
    # def create_request(self, method, data=None):
    #     request = self.client.generic(method, '/api/your_payment_endpoint/', json.dumps(data), content_type='application/json')
    #     request.user = self.user
    #     return request
    #
